﻿using Ecommerce.Domain.Dto;
using Ecommerce.Domain.Rabbit;
using EcommerceRabbit.Repository.Interface;

namespace EcommerceRabbit.Servies.traceability
{
    public class TraceServices : ITraceServices
    {
        private readonly ITraceRepository traceRepository;

        private readonly IImageRepository imageRepository;

        private readonly IClutivaReposiotry clutivaReposiotry;

        private readonly IImmuneRepository immuneRepository;

        private readonly IEnterpriseRepository enterpriseRepository;

        public TraceServices(ITraceRepository traceRepository, IImageRepository imageRepository, IClutivaReposiotry clutivaReposiotry, IImmuneRepository immuneRepository, IEnterpriseRepository enterpriseRepository)
        {
            this.traceRepository = traceRepository;
            this.imageRepository = imageRepository;
            this.clutivaReposiotry = clutivaReposiotry;
            this.immuneRepository = immuneRepository;
            this.enterpriseRepository = enterpriseRepository;
        }

        public async Task<int> AddTrace(TraceDTO traceDTO)
        {
            ImageRes image = new ImageRes();
            image.Images=traceDTO.Images;
            await imageRepository.AddAsync(image);
            var list = await imageRepository.GetAsync(w=>w.Images == traceDTO.Images);
            Traceability traceability = new Traceability();
            traceability.Mid = list.FirstOrDefault().Mid;
            traceability.Firm = traceDTO.Firm;
            traceability.license = traceDTO.license;
            traceability.TName = traceDTO.TName;
            traceability.GoodNo = traceDTO.GoodNo;
            traceability.Shelf =traceDTO.Shelf;
            return await traceRepository.AddAsync(traceability);
        }

        public async Task<List<Cultivation>> GetCultivations(int Tid)
        {
            return await clutivaReposiotry.GetAsync(w => w.Tid == Tid);
        }

        public async Task<List<Traceability>> GetTraceabilities()
        {
           return await traceRepository.GetAllAsync();
        }

        public async Task<List<TraceDTO>> GetTraceDTO(int Tid)
        {
            var list = from a in await traceRepository.GetAllAsync()
                       join b in await imageRepository.GetAllAsync() on a.Mid equals b.Mid
                       where a.Tid == Tid
                       select new TraceDTO
                       {
                           Tid = a.Tid,
                           Mid = b.Mid,
                           Shelf = a.Shelf,
                           license = a.license,
                           TName = a.TName,
                           Firm = a.Firm,
                           GoodNo = a.GoodNo,
                           Images = b.Images
                       };
            return list.ToList();
        }

        public async Task<int> UpdateTrace(TraceDTO traceDTO)
        {
            ImageRes image = new ImageRes();
            image.Images = traceDTO.Images;
            await imageRepository.AddAsync(image);
            var list = await imageRepository.GetAsync(w => w.Images == traceDTO.Images);
            Traceability traceability = new Traceability();
            traceability.Mid = list.FirstOrDefault().Mid;
            traceability.Firm = traceDTO.Firm;
            traceability.license = traceDTO.license;
            traceability.TName = traceDTO.TName;
            traceability.GoodNo = traceDTO.GoodNo;
            traceability.Shelf = traceDTO.Shelf;
            return await traceRepository.Update(traceability);
        }

        public async Task<List<Immune>> GetImmunes(int Cid)
        {
            return await immuneRepository.GetAsync(w => w.Cid == Cid);
        }

        public async Task<int> AddImmunes(Immune immune)
        {
            return await immuneRepository.AddAsync(immune);
        }

        public async Task<List<Enterprise>> GetEnterprises(int Cid)
        {
            return await enterpriseRepository.GetAsync(w => w.Cid == Cid);
        }
    }
}